//
//  ScheduleController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/26.
//  Copyright © 2016年 czd. All rights reserved.
//
#define DEVICEWIDTH [UIScreen mainScreen].bounds.size.width
#define DEVICEHEIGHT [UIScreen mainScreen].bounds.size.height

#import "ScheduleController.h"
#import "SchedulModel.h"
#import "ScheduleViewCell.h"

@interface ScheduleController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *dataArray2;
@property (nonatomic,strong) NSMutableArray *dataArray3;
@property (nonatomic,strong) NSMutableArray *dataArray4;
@property (nonatomic,strong) NSMutableArray *dataArray5;

@property (nonatomic,strong) UICollectionView *collec;
@property (nonatomic,strong) UITableView *tab;

@end

@implementation ScheduleController
static NSString *const reuserID = @"SchedulCell";
static NSString *const reuserID1 = @"SchedulCell1";
static NSString *const reuserID2 = @"SchedulCell2";
static NSString *const reuserID3 = @"SchedulCell3";
static NSString *const reuserID4 = @"SchedulCell4";
static NSString *const reuserID5 = @"SchedulCell5";
static NSString *const reuserID6 = @"SchedulCell6";

-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(NSMutableArray *)dataArray2{
    if (_dataArray2 == nil) {
        
        _dataArray2 = [NSMutableArray array];
    }
    return _dataArray2;
}

- (NSMutableArray *)dataArray3{
    if (_dataArray3 == nil) {
        
        _dataArray3 = [NSMutableArray array];
    }

    return _dataArray3;
}

-(NSMutableArray *)dataArray4{
    if (_dataArray4 == nil) {
        _dataArray4 = [NSMutableArray array];
    }
    return _dataArray4;
}

-(NSMutableArray *)dataArray5{
    if (!_dataArray5) {
        _dataArray5 = [NSMutableArray array];
    }
    return _dataArray5;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    UICollectionView *collec = [[UICollectionView alloc] initWithFrame:CGRectMake(10,10,DEVICEWIDTH - 20 , 200) collectionViewLayout:layout];
    layout.minimumLineSpacing = 10;
//    layout.itemSize = CGSizeMake(DEVICEWIDTH-20,300);
    
    collec.dataSource = self;
    collec.delegate = self;
    self.collec = collec;
    collec.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    [self.view addSubview:collec];
    
    [collec registerNib:[UINib nibWithNibName:@"ScheduleViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuserID];
//    [collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuserID3];
//    [collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuserID4];
//    [collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuserID5];
//    [collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuserID6];
    
    UITableView *tab = [[UITableView alloc] init];
    self.tab = tab;
    self.tab.delegate = self;
    self.tab.dataSource = self;
    tab.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    [self.view addSubview:tab];
    [tab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(collec.mas_bottom).mas_offset(10);
        make.left.equalTo(self.view.mas_left).mas_offset(10);
        make.right.equalTo(self.view.mas_right).mas_offset(-10);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(- 150);
    }];
    
    
//    __weak typeof (self) weakSelf = self;
//    
//    [self.collec addHeaderWithCallback:^{
//        
//        [weakSelf loadData]; //加载网络数据
//    }];
//    [self.collec headerBeginRefreshing];
    [self loadData];
//    [self loadData2];
//    [self loadData3];
//    [self loadData4];
//    [self loadData5];
}

//行程接口
- (void)loadData{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': tdfzMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];
        
        NSArray *dictArr = JSON[@"reason"];
        
        if (dictArr.count) {
            for (NSDictionary *dict in dictArr) {
                
                NSDictionary *dic = dict[@"attrs"];
                SchedulModel *model = [SchedulModel mj_objectWithKeyValues:dic];
                
                [self.dataArray addObject:model];

            }
            
        }
        [self.collec headerEndRefreshing];
        [self loadData2];
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

//行程安排接口

- (void)loadData2{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': xcapMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSDictionary *dict = JSON[@"reason"];
        
        NSDictionary *dict2 = dict[@"attrs"];
        
        if (dict2) {
            SchedulModel *model = [SchedulModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray5 addObject:model];

        }
//        [self.tab reloadData];
                [self loadData3];

    } failure:^(NSError *error) {
        
        
    }];
    
}

//出行须知
- (void)loadData3{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': cpxzMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSDictionary *dict = JSON[@"reason"];
        
        NSDictionary *dict2 = dict[@"attrs"];
        
        if (dict2) {
            SchedulModel *model = [SchedulModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray2 addObject:model];

        }
        
//        [self.tab reloadData];
                [self loadData4];

    } failure:^(NSError *error) {
        
        
    }];
    
}

//表团纪律
- (void)loadData4{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': tdjlMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSDictionary *dict = JSON[@"reason"];
        
        NSDictionary *dict2 = dict[@"attrs"];
        
        if (dict2) {
            SchedulModel *model = [SchedulModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray3 addObject:model];
        }
        
//        [self.tab reloadData];
        [self loadData5];
    } failure:^(NSError *error) {
        
        
    }];
    
}
//城市介绍
- (void)loadData5{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': csjsMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSDictionary *dict = JSON[@"reason"];
        
        NSDictionary *dict2 = dict[@"attrs"];
        if (dict2) {
            SchedulModel *model = [SchedulModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray4 addObject:model];
        }
        
        [self.tab reloadData];
                [self.collec reloadData];

    } failure:^(NSError *error) {
        
        
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    SchedulModel *model = self.dataArray[indexPath.row];
    
    ScheduleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuserID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    if (cell == nil) {
        cell = [ScheduleViewCell WithScheduleViewCell];
    }
    
    cell.model = model;
    
    return cell;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SchedulModel *model = self.dataArray[indexPath.row];

    CGFloat H = [OAToolBox sizebyText:model.ROUNTING font:15 minsWidth:20];
    
    return CGSizeMake(DEVICEWIDTH - 20, 123 + H);
    
}

#pragma mark -----Tab 的代理和数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0){
            //行程安排
        
                SchedulModel *modelX = self.dataArray5.lastObject;
        
                UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuserID3];
        
                if (cell == nil) {
        
                    cell = [[UITableViewCell alloc] init];
                }
        
                cell.backgroundColor = [UIColor whiteColor];
        
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 5,80,40)];
        
                label.text = @"行程安排";
        
                label.textColor = [UIColor blueColor];
        
                label.font = [UIFont systemFontOfSize:15];
        
                [cell.contentView addSubview:label];
        
                UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 4., 10)];
                vi.backgroundColor = [UIColor blueColor];
        
                [cell.contentView addSubview:vi];
        
                UIWebView *lbl2 = [[UIWebView alloc] init];
        lbl2.scrollView.scrollEnabled = NO;

                lbl2.backgroundColor = [UIColor whiteColor];
                NSString *str = [modelX.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        
                NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        
        
                [cell.contentView addSubview:lbl2];
        
                [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(label.mas_bottom);
                    make.left.equalTo(cell.contentView.mas_left);
                    make.right.equalTo(cell.contentView.mas_right);
                    make.bottom.equalTo(cell.contentView.mas_bottom);
                }];
                [lbl2 loadHTMLString:str2 baseURL:nil];
        
                return cell;
        
        
            }else if (indexPath.row == 1){
                SchedulModel *model1 = self.dataArray2.firstObject;
        
                UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuserID4];
        
                if (cell == nil) {
        
                    cell = [[UITableViewCell alloc] init];
                }
        
                cell.backgroundColor = [UIColor whiteColor];
        
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 5,80, 40)];
        
                label.text = @"出行须知";
                label.textColor = [UIColor blueColor];
                label.font = [UIFont systemFontOfSize:15];
        
                [cell.contentView addSubview:label];
        
                UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 4, 10)];
                vi.backgroundColor = [UIColor blueColor];
        
                [cell.contentView addSubview:vi];
        
                UIWebView *lbl2 = [[UIWebView alloc] init];
        
                lbl2.backgroundColor = [UIColor whiteColor];
                lbl2.scrollView.scrollEnabled = NO;

                NSString *str = [model1.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        
                NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        
                [lbl2 loadHTMLString:str2 baseURL:nil];
        
                [cell.contentView addSubview:lbl2];
        
                [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(label.mas_bottom);
                    make.left.equalTo(cell.contentView.mas_left);
                    make.right.equalTo(cell.contentView.mas_right);
                    make.bottom.equalTo(cell.contentView.mas_bottom);
                }];
        
                return cell;
        
        
            }else if (indexPath.row == 2){
                SchedulModel *model2 = self.dataArray3.firstObject;
        
                UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuserID5];
        
                if (cell == nil) {
        
                    cell = [[UITableViewCell alloc] init];
                }
        
                cell.backgroundColor = [UIColor whiteColor];
        
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 5,80, 40)];
        
                label.text = @"表团纪律";
        
                label.font = [UIFont systemFontOfSize:15];
                label.textColor = [UIColor blueColor];
                [cell.contentView addSubview:label];
        
                UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 4., 10)];
        
                vi.backgroundColor = [UIColor blueColor];
        
                [cell.contentView addSubview:vi];
        
                UIWebView *lbl2 = [[UIWebView alloc] init];
                lbl2.scrollView.scrollEnabled = NO;

                lbl2.backgroundColor = [UIColor whiteColor];
                NSString *str = [model2.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        
                NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        
                [lbl2 loadHTMLString:str2 baseURL:nil];
        
                [cell.contentView addSubview:lbl2];
        
                [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(label.mas_bottom);
                    make.left.equalTo(cell.contentView.mas_left);
                    make.right.equalTo(cell.contentView.mas_right);
                    make.bottom.equalTo(cell.contentView.mas_bottom);
                }];
        
                return cell;
        
            }else{
        
                SchedulModel *model3 = self.dataArray4.firstObject;
                UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuserID6];
        
                if (cell == nil) {
        
                    cell = [[UITableViewCell alloc] init];
                }
        
                cell.backgroundColor = [UIColor whiteColor];
        
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 5,80, 40)];
                
                label.text = @"城市介绍";
                label.textColor = [UIColor blueColor];
                label.font = [UIFont systemFontOfSize:15];
                
                [cell.contentView addSubview:label];
                
                UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 4, 10)];
                vi.backgroundColor = [UIColor blueColor];
                
                [cell.contentView addSubview:vi];
                
                UIWebView *lbl2 = [[UIWebView alloc] init];
                lbl2.scrollView.scrollEnabled = NO;

                lbl2.backgroundColor = [UIColor whiteColor];
                NSString *str = [model3.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
                
                NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
                
                [lbl2 loadHTMLString:str2 baseURL:nil];
                
                [cell.contentView addSubview:lbl2];
                
                [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(label.mas_bottom);
                    make.left.equalTo(cell.contentView.mas_left);
                    make.right.equalTo(cell.contentView.mas_right);
                    make.bottom.equalTo(cell.contentView.mas_bottom);
                }];
                
                return cell;
            
            }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        
        SchedulModel *model = self.dataArray5.lastObject;
        NSString *str = [model.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        
        NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        
        CGFloat H = [OAToolBox sizebyText:str2 font:15 minsWidth:20];
        
        return H + 10;
    }else if (indexPath.row == 1){
        SchedulModel *model1 = self.dataArray2.firstObject;
        NSString *str = [model1.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        
        NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        CGFloat H = [OAToolBox sizebyText:str2 font:15 minsWidth:20];
        
        return  H+ 10;
        
    }else if (indexPath.row == 2){
        SchedulModel *model2 = self.dataArray3.firstObject;
        NSString *str = [model2.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        
        NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        
        CGFloat H = [OAToolBox sizebyText:str2 font:15 minsWidth:20];
        
        return H+10;
    }else{
        SchedulModel *model3 = self.dataArray4.firstObject;
        NSString *str = [model3.SITUATION stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        
        NSString *str2 = [str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        CGFloat H = [OAToolBox sizebyText:str2 font:15 minsWidth:20];
        
        return  H+10;
    }



}



@end



